package com.itheima.Service.Impl;

import com.itheima.Service.MemberService;
import com.itheima.mapper.MemberMapper;
import com.itheima.pojo.Member;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjuster;
import java.time.temporal.TemporalAdjusters;
import java.util.ArrayList;
import java.util.List;

@Service
public class MemberServiceImpl implements MemberService {

    @Autowired
    private MemberMapper memberMapper;

    @Override
    public List<Integer> findMemberCountByMonth(List<String> list) {
        List<Integer> memberCounts = new ArrayList<>();
        // 日期格式化器
        DateTimeFormatter dateTimeFormat = DateTimeFormatter.ofPattern("yyyy-MM");
        for (String s : list) {
            // 将月份字符串解析为LocalDate
            LocalDate firstDayOfMonth  = LocalDate.parse(s + "-01",DateTimeFormatter.ofPattern("yyyy-MM-dd"));
            // 获取该月的最后一天
            LocalDate lastDayOfMonth = firstDayOfMonth.with(TemporalAdjusters.lastDayOfMonth());

            // 传入该月最后一天，获取在此日期之前的会员数量
            Integer count = memberMapper.findMemberCountBeforeDate(lastDayOfMonth);
            memberCounts.add(count);

        }
        return memberCounts;
    }
}
